package jrmob.yonyou.com.commonlib.app_config_tool.tool.base;

import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by zhang on 2016/9/24.
 * AES加密算法工具类
 */

public class AESUtil {


    public static  byte [] sk = null;


    /**
     * 得到编码后的key
     * @return
     */
    public static String getSkStr() {
        if (null == sk){
            sk = generatorKey();
        }
        return Base64.encodeToString(sk, 0);
    }


    public static byte[] getSkBytes() {
        if (null == sk){
            sk = generatorKey();
        }
        return sk;
    }







    /**
     * 生成key
     *
     * @return
     */
    public static byte[] generatorKey() {
        if (sk == null){
            try {
                KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); // 说明是什么算法
                keyGenerator.init(128);
                SecretKey key = keyGenerator.generateKey();
                return key.getEncoded();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            return sk;
        }
        return  sk;
    }


    /**
     * 加密
     *
     * @param
     * @param data
     * @return
     */
    public static byte[] enCodeBytes( String data) {

        SecretKey secretKey = new SecretKeySpec(AESUtil.getSkBytes(), "AES");

        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 加密
            byte[] cipherBytes = cipher.doFinal(data.getBytes());
            return cipherBytes;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 加密
     *
     * @param
     * @param data
     * @return
     */
    public static String enCodeStr(String data) {

        String tempString = null;
        byte[] b = enCodeBytes(data);
        if (null != b){
            tempString = Base64.encodeToString(b, 0); // 加密后再encode-base64
        }
        return tempString;

    }

    /**
     * 加密
     *
     * @param
     * @param data
     * @return
     */
//    public static String enCodeBytes(String data) {
//
//        SecretKey secretKey = new SecretKeySpec(AESUtil.getSkBytes(), "AES");
//
//        try {
//            Cipher cipher = Cipher.getInstance("AES");
//            cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 加密
//            byte[] cipherBytes = cipher.doFinal(data.getBytes());
//            return new String(cipherBytes);
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//
//    }


    /**
     * 解密
     *
     * @param key
     * @param data
     * @return
     */
    public static String deCode(byte[] key, byte[] data) {
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] encrypted = cipher.doFinal(data);
            return new String(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
